import VirtualBackgroundEffect from './virtual-background/virtual-background';
const IRtc = ivcs.IRtc;
let { MediaStreamFactory, StreamSourceInfo } = IRtc.Base;

export interface IBackgroundOption {
	type: string;
	url: string;
	blurRadius?: number;
}
export interface IWatermarkOption {
	type: string;
	url: string;
}
const nopeurl = './static/background/img/background/nope.png';
class BackgroundEffect {
	webarEffect?: VirtualBackgroundEffect;
	virtualOption: { type: string; url: string };
	watermarkOption: { type: string; url: string };
	videoDeviceId: string | undefined;
	audioDeviceId: string | undefined;

	constructor() {
		this.webarEffect = undefined;
		this.virtualOption = { type: 'no', url: nopeurl };
		this.watermarkOption = { type: 'no', url: nopeurl };
		this.videoDeviceId = undefined;
		this.audioDeviceId = undefined;
	}

	async createEffectStream(agentLabel: string, videoId: string, createBackground: IBackgroundOption, createWatermark: IWatermarkOption, videoDeviceId?: string, audioDeviceId?: string, defaultMediaOptions?: any) {
		if (!createBackground && !createWatermark) return;
		let defaultWatermark = { type: 'no', url: '' };
		let defaultBackground = { type: 'no', url: '' };
		let background = { ...defaultWatermark, ...createBackground };
		let watermark = { ...defaultBackground, ...createWatermark };
		if (
			this.virtualOption.type === 'no' &&
			background.type === 'no' &&
			this.watermarkOption.type === 'no' &&
			watermark.type === 'no' &&
			this.videoDeviceId === videoDeviceId &&
			this.audioDeviceId === audioDeviceId
		) {
			// console.warn('createEffectStream : ', background, watermark);
			return;
		}
		if (
			this.virtualOption.type === background.type &&
			this.virtualOption.url === background.url &&
			this.watermarkOption.type === watermark.type &&
			this.watermarkOption.url === watermark.url &&
			this.videoDeviceId === videoDeviceId &&
			this.audioDeviceId === audioDeviceId
		) {
			// console.warn('createEffectStream : ', background, watermark);
			return;
		}

		this.virtualOption.type = background.type;
		this.virtualOption.url = background.url;
		this.watermarkOption.type = watermark.type;
		this.watermarkOption.url = watermark.url;
		this.videoDeviceId = videoDeviceId;
		this.audioDeviceId = audioDeviceId;
		// release effect
		this.stopEffect();

		const audioDeviceID = audioDeviceId || defaultMediaOptions.cameraStreamAudioConstraints.deviceId;
		const videoDeviceID = videoDeviceId || defaultMediaOptions.cameraStreamVideoConstraints.deviceId;
		const { resolution, frameRate } = defaultMediaOptions.cameraStreamVideoConstraints;

		const webarEffect = new VirtualBackgroundEffect();
		const stream = await webarEffect.createCustomizedStream({ background: background, watermark: watermark }, videoId, resolution, frameRate, agentLabel, videoDeviceID, audioDeviceID, false);
		console.log('webarEffect:', stream.customizedLocalStream.mediaStream);

		this.webarEffect = webarEffect as any;
		return stream.customizedLocalStream;
	}

	async createmediaStream(agentLabel: string, videoDeviceId?: string, audioDeviceId?: string, defaultMediaOptions?: any) {
		if (this.virtualOption.type === 'no' && this.watermarkOption.type === 'no') {
			return;
		}

		this.virtualOption.type = 'no';
		this.virtualOption.url = nopeurl;
		this.watermarkOption.type = 'no';
		this.watermarkOption.url = nopeurl;
		// release effect
		this.stopEffect();

		let mediaStreamConstraints = {
			audio: defaultMediaOptions.cameraStreamAudioConstraints,
			video: defaultMediaOptions.cameraStreamVideoConstraints
		};
		if (audioDeviceId) {
			mediaStreamConstraints.audio = { deviceId: audioDeviceId };
		}
		if (videoDeviceId) {
			mediaStreamConstraints.video = { deviceId: videoDeviceId };
		}
		let videoSource = 'camera';
		let audioSource = 'mic';
		let streamSourceInfo = new StreamSourceInfo(audioSource, videoSource);

		if (!this.webarEffect) {
			this.webarEffect = new VirtualBackgroundEffect();
		}

		const stream = await this.webarEffect.createWebRtcMediaStream(mediaStreamConstraints, streamSourceInfo, { type: agentLabel }, false);
		console.log('webarEffect:', stream);
		return stream;
	}

	stopEffect(reset = false) {
		if (this.webarEffect) {
			this.webarEffect.stopEffect();
			this.webarEffect = undefined;
		}
		if (reset) {
			this.virtualOption = { type: 'no', url: nopeurl };
			this.watermarkOption = { type: 'no', url: nopeurl };
		}
	}
}

const backgroundEffect = new BackgroundEffect();

export default backgroundEffect;
